package com.srmt.spring.autoconfigure;

import com.srmt.document.api.DocumentService;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * Document Service 健康检查指示器
 * 集成 Spring Boot Actuator 健康检查
 * 
 * @author SRMT Development Team
 * @since 2.0.0
 */
@Component("ocumentHealthIndicator")
@ConditionalOnClass(HealthIndicator.class)
public class DocumentHealthIndicator implements HealthIndicator {
    
    private final DocumentService documentService;
    
    public DocumentHealthIndicator(DocumentService documentService) {
        this.documentService = documentService;
    }
    
    @Override
    public Health health() {
        try {
            // 尝试获取各个组件，验证服务是否可用
            boolean managerAvailable = documentService.getDocumentManager() != null;
            boolean writerAvailable = documentService.getContentWriter() != null;
            boolean readerAvailable = documentService.getContentReader() != null;
            
            if (managerAvailable && writerAvailable && readerAvailable) {
                return Health.up()
                        .withDetail("service", "Document Service")
                        .withDetail("status", "All components available")
                        .withDetail("documentManager", "Available")
                        .withDetail("contentWriter", "Available")
                        .withDetail("contentReader", "Available")
                        .build();
            } else {
                return Health.down()
                        .withDetail("service", "Document Service")
                        .withDetail("status", "Some components unavailable")
                        .withDetail("documentManager", managerAvailable ? "Available" : "Unavailable")
                        .withDetail("contentWriter", writerAvailable ? "Available" : "Unavailable")
                        .withDetail("contentReader", readerAvailable ? "Available" : "Unavailable")
                        .build();
            }
        } catch (Exception e) {
            return Health.down()
                    .withDetail("service", "Document Service")
                    .withException(e)
                    .build();
        }
    }
}